package com.kye.express.business.order;

import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.base.BaseAppCompatActivity;
import com.kye.express.databinding.ActivityProtocolBinding;
import com.kye.express.utils.ToolbarManager;
import com.kye.model.BuildConfig;
import com.kye.webview.WebViewFragment;

import io.reactivex.functions.Consumer;

public class ProtocolActivity extends BaseAppCompatActivity{

    public static final int PROTOCOL_ACTIVITY_REQUEST_CODE = 1;
    public static final int PROTOCOL_ACTIVITY_RESULT_CODE = 1;

    private ActivityProtocolBinding mBinding;

    public static void startActivityForResult(Activity activity) {
        activity.startActivityForResult(new Intent(activity,ProtocolActivity.class), PROTOCOL_ACTIVITY_REQUEST_CODE);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_protocol);

        initEvent();
    }

    private void initEvent() {
        ToolbarManager.setToolbar(this, mBinding.toolbar);

        RxView.clicks(mBinding.buttonAgree)
                .compose(new DebounceObservableTransformer<>())
                .compose(bindToLifecycle())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        setResult(PROTOCOL_ACTIVITY_RESULT_CODE);
                        finish();
                    }
                });

        WebViewFragment webViewFragment = WebViewFragment.newInstance(BuildConfig.HOST_RES + "h5/help/22.html", true);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_layout_container, webViewFragment).commit();
    }
}
